# -*- coding: UTF-8 -*-


import os
import random
from os import path

nameSet = set()


def getRandomInt():
    ret = random.randint(1, 100000)
    if not nameSet.__contains__(ret):
        nameSet.add(ret)
        return ret
    else:
        getRandomInt()


def renameAllSampledata(sampleRootPath):
    '''
    用随机数重命名指定目标文件夹下的所有文件
    :param sampleRootPath:
    :return:
    '''
    dirs = os.listdir(sampleRootPath)

    for dir in dirs:
        if not dir.startswith("."):
            sub_paths = os.path.join(sampleRootPath, dir)
            print(dir)
            sub_dirs = os.listdir(sub_paths)
            for subdir in sub_dirs:
                file_abs_path = sub_paths + os.path.sep + subdir
                new_name = sub_paths + os.path.sep + str(getRandomInt()) + ".png"
                print(file_abs_path + " ->  " + new_name)
                os.rename(file_abs_path, new_name)


if __name__ == '__main__':
    samplePath = "../validatecode_dec/sample"
    currentPath = path.dirname(__file__)
    sampleAbsPath = os.path.join(currentPath, samplePath)
    renameAllSampledata(sampleAbsPath)
